//
//  WBHomeViewController.swift
//  weibo
//
//  Created by chenguanglei on 16/11/25.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SVProgressHUD

let  WBHomeViewControllerIdentifier = "WBHomeViewControllerIdentifier"

class WBHomeViewController: WBBaseTableViewController {
    
    var statusListViewModel : WBStatusListViewModel = WBStatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        
        if isLogin {
            
            SVProgressHUD.show()
            
            tableView.register(WBHomeTableViewCell.self, forCellReuseIdentifier: WBHomeViewControllerIdentifier)

            tableView.rowHeight = UITableViewAutomaticDimension
            // 第二个属性就是预估行高 -- 随便写
            tableView.estimatedRowHeight = 200
            loadData()
            
            tableView.separatorStyle = .none
            
            tableView.backgroundColor = UIColor(white: 240 / 255, alpha: 1)
            
            
            tableView.tableFooterView = pullView
            
            
            tableView.addSubview(refreshView)
            
            refreshView.addTarget(self, action: #selector(loadData), for: UIControlEvents.valueChanged)
            
            
            
        }else {
            
            visitorView.setupVisitor(title: "关注一些人，回这里看看有什么惊喜", imageNamge: "visitordiscover_feed_image_house", isHidden:false)
            
        }
        
        
    }
    
    //加载数据
    @objc private func loadData(){
        
        
        statusListViewModel.loadData(isPulling: pullView.isAnimating) {(count:Int)->() in
            
            SVProgressHUD.dismiss()
            
            self.pullView.stopAnimating()
            
            self.tableView.reloadData()
            
            
            self.showNotice(count: count)
            
            self.refreshView.endRefreshing()
            
           
        }
        
        
    }
    
    
    //下拉刷新提示
    private func showNotice(count : Int){
    
        let labelH : CGFloat = 44
        
        let label = UILabel(labelText: "更新\(count)条微博", labelFont: 16, textColor: UIColor.gray, align: NSTextAlignment.center, isSingleLine: false)
        
        if count == 0 {
            
            label.text = "暂时没有更新微博"
            
        }
        
        label.backgroundColor = UIColor.orange
        label.frame = CGRect.init(x: 0, y: -labelH, width: kUIScreenWidth, height: labelH)
        
        self.tableView.addSubview(label)
        
        
//        label.frame = CGRect.init(x: 0, y: 64-labelH, width: kUIScreenWidth, height: labelH)
//        self.navigationController?.view.insertSubview(label, belowSubview:(navigationController?.navigationBar)!)
        
        
        
        UIView.animate(withDuration: 1.0, animations: { 
            
            label.transform = CGAffineTransform(translationX: 0, y: +labelH)
            
            }) { (Bool) in
                
                sleep(1)
                
                UIView.animate(withDuration: 1.0, animations: {
                    
                    label.transform = CGAffineTransform.identity
                    
                    }, completion: { (Bool) in
                        
                        label.removeFromSuperview()
                        
                })
                
                
        }
    
    }
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        
        return statusListViewModel.array.count
    }
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell : WBHomeTableViewCell = tableView.dequeueReusableCell(withIdentifier: WBHomeViewControllerIdentifier, for: indexPath) as! WBHomeTableViewCell
        
            cell.selectionStyle = .none
        
        if indexPath.row == (statusListViewModel.array.count - 1 ){
            
            pullView.startAnimating()
            
            loadData()
            
        }
        
        
        cell.homeViewModel = statusListViewModel.array[indexPath.row]
        
        return cell
        
    }
    
    
    private lazy var pullView: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
    
    private lazy var refreshView: WBRefreshView = WBRefreshView()
    
    
    
}
